package com.woniu.product.service.impl;

import com.woniu.entity.Result;
import com.woniu.product.mapper.ProductCommentMapper;
import com.woniu.product.pojo.ProductComment;
import com.woniu.product.entity.dto.ReplyDTO;
import com.woniu.product.pojo.vo.ProductCommentVo;
import com.woniu.product.service.IProductCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品评价表 服务实现类
 * </p>
 *
 * @author LiFangyu
 * @since 2023-02-15
 */
@Service
public class ProductCommentServiceImpl extends ServiceImpl<ProductCommentMapper, ProductCommentVo> implements IProductCommentService {

    @Autowired(required = false)
    private ProductCommentMapper productCommentMapper;


    @Override
    public List<ProductComment> listAllComment(String id) {
        return productCommentMapper.listById(id);
    }

    @Override
    public List<ProductCommentVo> listAllTree() {
        // 1. 查询出所有的第一分类
        List<ProductCommentVo> productCommentOneList = productCommentMapper.selectByParentId("");

        // 2. 遍历一级分类
        if(productCommentOneList != null && productCommentOneList.size() > 0){
            for(ProductCommentVo one : productCommentOneList){
                // 3. 通过一级分类去查询对应的二级分类
                List<ProductCommentVo> productCommentTwoList = productCommentMapper.selectByParentId(one.getCommentId());
                // 4. 将查询出来的二级分类数据设置到一级分类里面
                one.setChildren(productCommentTwoList);
            }
        }
        // 5. 返回一级分类
        return productCommentOneList;
    }

    @Override
    public Result saveReply(ReplyDTO replyDTO) {
        productCommentMapper.saveReply(replyDTO);
        return Result.ok();
    }
}
